//保存表格的实例对象
var sx = null;

//调用封装方法
getTable();

var insider = null;
var coupons = null;
$(function (){

    console.log('回调函数')
    $('#user').hide()
    $('#coupons').hide()
    insider = localStorage.getItem('user');
    coupons = localStorage.getItem('coupons');
    console.log('coupons : '+ coupons)
    console.log('insider : '+ insider)
    if (coupons == 2){
        $('#user').show()
    }else {
        $('#coupons').show()
    }

})



// ---------------------------------------------------
//方法区

//封装方法重载表格
function sxTable(){
    //通过表格id参数  重载刷新
    sx.reload()
}


//封装渲染表格
function getTable(){
    layui.use(['table','layer','jquery','form',],function() {
        //模板保存到变量上
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.$;


        sx = table.render({
            elem:"#demo",
            url:"/YltUser/UserManage/getUserList",
            where:{
                insider : localStorage.getItem('user')
            },
            page:{
                layout:['count','prev','page','next','limit'],
                groups:5,
                limit:10,
                limits:[5,10,20]
            },
            cols:
                [[
                    {checkbox:true},
                    // {field: 'courseLogId', title: '序号',width: 100,align: 'center'},
                    {field: 'userId', title: '用户编号',width: 150,align: 'center',templet:function (d){
                            return d.user.userId;
                    }},
                    {field: 'userUrl', title: '用户头像',width: 150,align: 'center',templet:function (d){
                            return '<img src="http://43.143.176.39/'+ d.user.userUrl +' " width="100px" height="100px" style="border-radius: 30px"; border="2 solid blue" > '
                        }},
                    {field: 'userAccount', title: '用户账号', width:200,align: 'center',templet:function (d){
                            return d.user.userAccount;
                        }},
                    {field: 'userName', title: '用户昵称',width: 200,align: 'center',templet:function (d){
                            return d.user.userName;
                    }},
                    {field: 'userCreateDate', title: '用户注册时间', width: 200,align: 'center',templet:function (d){
                            return d.user.userCreateDate;
                    }}
                ]],
            toolbar:'#toolbardemo',
            skin: 'line',
            parseData:function (res){
                return{
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
            }

        }),



            //监听表格头部
            table.on('toolbar(filterDemo)',function (obj){
                var event = obj.event
                var checkStatus = table.checkStatus(obj.config.id);
                var data =  checkStatus.data;
                // console.log(event)
                // console.log(checkStatus)
                // console.log(data)

                if (obj.event == 'add'){
                    if (data.length == 0){
                        layer.msg('至少选择一位用户',{icon:5,time:1000})
                    }else {
                        var array = [];
                        $.each(data,function (){
                            // console.log(this)
                            array.push(this.user.userId)
                        })
                        console.log(array)
                        //发送ajax添加为进思用户
                        $.ajax({
                            type:'get',
                            url:'/YltUser/UserManage/upUserInsider',
                            data:{array:array.toString()},
                            dataType:'json',
                            success:function (res){
                                console.log(res)
                                if (res.state == '200'){
                                    //先得到当前iframe层的索引
                                    layer.msg("添加为进思用户成功",{time: 1500,icon:6},function (){
                                        let index = parent.layer.getFrameIndex(window.name);
                                        //再执行关闭
                                        parent.layer.close(index);
                                        //刷新主页面
                                        //调用自己写的重载方法
                                        parent.sxTable();
                                    })

                                }else {
                                    layer.msg("修改用户信息失败",{time: 1500,icon:5},function (){
                                        let index = parent.layer.getFrameIndex(window.name);
                                        //再执行关闭
                                        parent.layer.close(index);
                                    });
                                }
                            }
                        })
                    }
                }
                else if (obj.event == 'gx'){
                    if (data.length == 0){
                        layer.msg('至少选择一位用户',{icon:5,time:1000})
                    }else {
                        var array = [];
                        $.each(data,function (){
                            // console.log(this)
                            array.push(this.user.userId)
                        })
                        console.log(array)
                        layer.open({
                            type:2,
                            title:'勾选优惠券',
                            content:'couponTable.html',
                            shade:[1,'#37444B'],
                            anim:4,
                            area:['100%','100%'],
                            //弹出层成功弹出后的回调函数
                            success:function (layero,index) {
                                //父页面向子页面传参 -- 向iframe层传数据
                                //获取iframe页面
                                var body = layer.getChildFrame('body', index);
                                body.find('#userArray').val(array.toString());
                            },
                            end:function (){
                                let index = parent.layer.getFrameIndex(window.name);
                                //再执行关闭
                                parent.layer.close(index);
                                //调用自己写的重载方法
                                parent.sxTable();
                            }
                        })
                    }
                }
            })
    })

}